package com.mangatta.ec2.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;

public class Util {

    /**
     * Key for shared preferences.
     */
    //private static final String SHARED_PREFS = "MANGATTA_PREFS";
    public static final String AWS_ACCESS_ID = "awsAccessId";
    public static final String AWS_SECRET_KEY = "awsSecretKey";
    public static final String PULL_INTERVAL_IN_SECONDS = "pullIntervalKey";
    public static final String PULL_ENABLED = "pullKey";
    public static final String INSTANCE_ID = "instanceId";
    public static final int DEFAULT_PULL_INTERVAL_IN_SECONDS = 60; // one minute

    /*
     * Helper method to get a SharedPreferences instance.
     */
    public static SharedPreferences getSharedPreferences(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static boolean isAwsCredentialKey(String key) {
        return AWS_SECRET_KEY.equals(key) || AWS_ACCESS_ID.equals(key);
    }

    public static boolean isEmpty(String string) {
        return string == null || string.trim().length() == 0;
    }

    public static AWSCredentials getAWSCredentails(Context context) {
        SharedPreferences prefs = getSharedPreferences(context);
        String accessId = prefs.getString(Util.AWS_ACCESS_ID, null);
        String secretKey = prefs.getString(Util.AWS_SECRET_KEY, null);
        if (accessId != null && secretKey != null) {
            return new BasicAWSCredentials(accessId, secretKey);
        }
        return null;
    }

    public static boolean isAWSCredentialsAvailable(Context context) {
        return getAWSCredentails(context) != null;
    }

    public static boolean isPullEnabled(Context context) {
        SharedPreferences prefs = getSharedPreferences(context);
        return prefs.getBoolean(Util.PULL_ENABLED, true);
    }

    public static int getPullIntervalInSeconds(Context context) {
        try {
            SharedPreferences preferences = getSharedPreferences(context);
            return Integer.parseInt(preferences.getString(Util.PULL_INTERVAL_IN_SECONDS, null));
        } catch (Exception e) {
            return Util.DEFAULT_PULL_INTERVAL_IN_SECONDS;
        }
    }

	public static boolean isEmpty(Double threshold) {
		return threshold == null || threshold.doubleValue() == 0d;
	}
}
